package vn.com.vega.chacha.v2.fragment;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.ActivityBase;
import vn.com.vega.chacha.v2.Appinstant;
import vn.com.vega.chacha.v2.MainScreen;
import vn.com.vega.music.clientserver.JsonAlbum;
import vn.com.vega.music.clientserver.JsonSong;
import vn.com.vega.music.clientserver.JsonVideo;
import vn.com.vega.music.utils.Const;
import vn.com.vega.music.view.adapter.AlbumListAdapter;
import vn.com.vega.music.view.adapter.SongListAdapter;
import vn.com.vega.music.view.adapter.VideoListAdapter;
import vn.com.vega.music.view.custom.ChaChaDialog;
import vn.com.vega.music.view.custom.LoadingView;
import vn.com.vega.music.view.custom.LoadingView.RetryListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class ListChartFeatureFragment extends Fragment implements Const {
	private static final int MUSIC_CHART = 0;
	private static final int VIDEO_CHART = 1;
	private static final int ALBUM_CHART = 2;
	private ArrayList<String> fillterLabel;

	private int currentChart = MUSIC_CHART;

	private MainScreen mMainScreen;
	private Appinstant mApp;
	private SongListAdapter mSongListAdapter;
	private VideoListAdapter mVideoListAdapter;
	private AlbumListAdapter mAlbumListAdapter;
	private ListView mListView;
	private LoadingView mLoadingView;
	private View mFillterView;
	private TextView mFillterLabel;

	private LoadingChart mLoader;
	private ChaChaDialog mSelectChartDialog;

	public static Fragment newInstance() {
		ListChartFeatureFragment fragment = new ListChartFeatureFragment();
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View root = inflater.inflate(R.layout.list_chart_feature, null);
		mListView = (ListView) root.findViewById(R.id.lv_list_song);
		mLoadingView = (LoadingView) root
				.findViewById(R.id.loadingView_loading_list_feature);
		mFillterView = root.findViewById(R.id.tv_type_fillter);
		mFillterLabel = (TextView) root
				.findViewById(R.id.tv_search_fillter_category);
		fillterLabel = new ArrayList<String>();
		fillterLabel.add(getString(R.string.fillter_music));
		fillterLabel.add(getString(R.string.fillter_video));
		fillterLabel.add(getString(R.string.fillter_album));
		mFillterLabel.setText(fillterLabel.get(0));
		mLoadingView.setRetryListener(retryListener);
		mFillterView.setOnClickListener(onSelectChartListener);
		return root;
	}

	View.OnClickListener onSelectChartListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showChartSelect();
		}
	};

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mMainScreen = (MainScreen) getActivity();
		mApp = (Appinstant) mMainScreen.getApplicationContext();
	}

	private static final String DIALOG_SELECT_CHART_NAME = "dialog select chanrt";
	AdapterView.OnItemClickListener selectChartClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			mMainScreen.hideDialogFillter(DIALOG_SELECT_CHART_NAME);
			mFillterLabel.setText(fillterLabel.get(position));
			if (position == 0) {
				setChart(MUSIC_CHART);
			} else if (position == 1) {
				setChart(VIDEO_CHART);
			} else {
				setChart(ALBUM_CHART);
			}
		}
	};

	private void showChartSelect() {
		mMainScreen.showFilltter(fillterLabel, selectChartClickListener,
				DIALOG_SELECT_CHART_NAME);
	}

	private void showChart() {
		switch (currentChart) {
		case MUSIC_CHART:
			mMainScreen.trackPageView("Music chart");
			if (mSongListAdapter == null || mSongListAdapter.getCount() == 0) {
				if (mLoader != null) {
					mLoader.cancel(true);
				}
				mLoader = new LoadingChart(this, MUSIC_CHART);
				mLoader.execute();
			} else {
				mLoadingView.loadingFinish(true, null);
				mListView.setAdapter(mSongListAdapter);
			}
			break;
		case VIDEO_CHART:
			mMainScreen.trackPageView("Video chart");
			if (mVideoListAdapter == null || mVideoListAdapter.getCount() == 0) {
				if (mLoader != null) {
					mLoader.cancel(true);
				}
				mLoader = new LoadingChart(this, VIDEO_CHART);
				mLoader.execute();
			} else {
				mLoadingView.loadingFinish(true, null);
				mListView.setAdapter(mVideoListAdapter);
			}
			break;
		case ALBUM_CHART:
			mMainScreen.trackPageView("Album chart");
			if (mAlbumListAdapter == null || mAlbumListAdapter.getCount() == 0) {
				if (mLoader != null) {
					mLoader.cancel(true);
				}
				mLoader = new LoadingChart(this, ALBUM_CHART);
				mLoader.execute();
			} else {
				mLoadingView.loadingFinish(true, null);
				mListView.setAdapter(mAlbumListAdapter);
			}
			break;

		default:
			break;
		}
	}

	private void setChart(int chartType) {
		if (chartType != currentChart) {
			currentChart = chartType;
			mFillterLabel.setText(fillterLabel.get(currentChart));
			showChart();
		}
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		showChart();
	}

	RetryListener retryListener = new RetryListener() {

		@Override
		public void onRetry() {
			// TODO Auto-generated method stub
			showChart();
		}
	};

	static class LoadingChart extends AsyncTask<Void, Void, Void> {
		WeakReference<ListChartFeatureFragment> listChartFragmentRfc;
		int chartType;

		public LoadingChart(ListChartFeatureFragment base, int chart) {
			listChartFragmentRfc = new WeakReference<ListChartFeatureFragment>(
					base);
			chartType = chart;
		}

		private boolean checkActIsLive() {
			ListChartFeatureFragment fragment = listChartFragmentRfc.get();
			if (fragment == null) {
				return false;
			}
			if (fragment.mMainScreen instanceof ActivityBase) {
				if ((fragment.mMainScreen).isDestroy) {
					return false;
				}
			}
			return true;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			if (!checkActIsLive()) {
				return null;
			}
			ListChartFeatureFragment fragment = listChartFragmentRfc.get();
			switch (chartType) {
			case MUSIC_CHART:
				JsonSong jsonSong = JsonSong.loadBillboardSong();
				if (jsonSong.isSuccess()) {
					fragment.mApp.listMusicChartFeature.addAll(jsonSong.songs);
				}
				break;
			case VIDEO_CHART:
				JsonVideo jsonVideo = JsonVideo.loadBillboardVideo();
				if (jsonVideo.isSuccess()) {
					fragment.mApp.listVideoChartFeature
							.addAll(jsonVideo.videos);
				}
				break;
			case ALBUM_CHART:
				JsonAlbum jsonAlbum = JsonAlbum.loadBillboardAlbum();
				if (jsonAlbum.isSuccess()) {
					fragment.mApp.listAlbumChartFeature
							.addAll(jsonAlbum.albums);
				}
				break;

			default:
				break;
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (isCancelled() || !checkActIsLive()) {
				return;
			}
			ListChartFeatureFragment chartFeatureFragment = listChartFragmentRfc
					.get();
			switch (chartType) {
			case MUSIC_CHART:
				if (chartFeatureFragment.mApp.listMusicChartFeature.size() == 0) {
					chartFeatureFragment.mLoadingView.loadingFinish(false,
							"Bảng xếp hạng bài hát trống!");
				} else {
					chartFeatureFragment.mSongListAdapter = new SongListAdapter(
							chartFeatureFragment.mMainScreen,
							chartFeatureFragment.mApp.listMusicChartFeature,
							false, null);
					chartFeatureFragment.mSongListAdapter.setChart(true);
					chartFeatureFragment.mLoadingView.loadingFinish(true, null);
					chartFeatureFragment.mListView
							.setAdapter(chartFeatureFragment.mSongListAdapter);
				}
				break;
			case VIDEO_CHART:
				if (chartFeatureFragment.mApp.listVideoChartFeature.size() == 0) {
					chartFeatureFragment.mLoadingView.loadingFinish(false,
							"Bảng xếp hạng video trống!");
				} else {
					chartFeatureFragment.mVideoListAdapter = new VideoListAdapter(
							chartFeatureFragment.mMainScreen,
							chartFeatureFragment.mApp.listVideoChartFeature,
							false);
					chartFeatureFragment.mLoadingView.loadingFinish(true, null);
					chartFeatureFragment.mListView
							.setAdapter(chartFeatureFragment.mVideoListAdapter);
				}
				break;
			case ALBUM_CHART:
				if (chartFeatureFragment.mApp.listAlbumChartFeature.size() == 0) {
					chartFeatureFragment.mLoadingView.loadingFinish(false,
							"Bảng xếp hạng album trống!");
				} else {
					chartFeatureFragment.mAlbumListAdapter = new AlbumListAdapter(
							chartFeatureFragment.mMainScreen,
							chartFeatureFragment.mApp.listAlbumChartFeature);
					chartFeatureFragment.mLoadingView.loadingFinish(true, null);
					chartFeatureFragment.mListView
							.setAdapter(chartFeatureFragment.mAlbumListAdapter);
				}
				break;

			default:
				break;
			}
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			if (!checkActIsLive()) {
				return;
			}
			ListChartFeatureFragment fragment = listChartFragmentRfc.get();
			fragment.mLoadingView.startLoading();
		}
	}
}
